﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EricBlog.Models;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace EricBlog.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class articleController : ControllerBase
    {
        private readonly Join _Join;
        public articleController(Join join)
        {
            _Join = join;
        }
        [HttpGet]
        public string GetArticle()
        {
            var articles = _Join.article.Select(r => r.articles).FirstOrDefault();
            return articles;
        }
        [HttpGet]
        public Array GetAllArticle()
        {
            var all = _Join.article.Where(r => r.id<=4).ToArray();
            return all;
        }
        [HttpGet]
        public Array GetArticleByClassLife(int page)
        {
            var all = _Join.articlelife.Where(r => r.id > (page * 4 - 4) && r.id <= (page * 4)).ToArray();
            return all;
        }
        [HttpGet]
        public Array GetArticleByClassMark(int page)
        {
            var all = _Join.articlemark.Where(r => r.id > (page * 4 - 4) && r.id <= (page * 4)).ToArray();
            return all;
        }
        [HttpGet]
        public Array GetArticleByClassFront(int page)
        {
            var all = _Join.articlefront.Where(r => r.id > (page * 4 - 4) && r.id <= (page * 4)).ToArray();
            return all;
        }
        [HttpGet]
        public Array GetArticleByClassBack(int page)
        {
            var all = _Join.articleback.Where(r =>r.id>(page*4-4) && r.id<=(page*4)).ToArray();
            return all;
        }
        [HttpGet]
        public Array GetArticleBackPage()
        {
            var articles = _Join.articleback.OrderBy(r=>r.id).Skip(3).Take(3).ToArray();
            return articles;
        }
        [HttpGet]
        public int GetLastId(string c)
        {
            int num=0;
            switch (c){
                case "life":
                    num = _Join.articlelife.Select(r => r.id).ToArray().LastOrDefault();
                    break;
                case "mark":
                    num = _Join.articlemark.Select(r => r.id).ToArray().LastOrDefault();
                    break;
                case "front":
                    num = _Join.articlefront.Select(r => r.id).ToArray().LastOrDefault();
                    break;
                case "back":
                    num = _Join.articleback.Select(r => r.id).ToArray().LastOrDefault();
                    break;
            }
            return num;
        }
    }
}
